<template>
	<view class="pay-page home-wrap" :style="{ 'background':'#F5F4F9'}">
		<shopro-navbar :isBack="true" :background="{'background':'#F5F4F9'}">
			<text slot="content" class="u-flex u-flex-center u-flex-items-center" style="color: #000; width: 85%; text-align: center;padding-left: 20rpx;height: 100%;">订单支付</text>
		</shopro-navbar>
		<!-- 待付款状态 -->
		<view class="status-section">
			<view class="status-row">
				<uni-icons type="info" color="#3478f6" size="22" />
				<text class="status-title">待付款</text>
			</view>
			<view class="countdown-row">
				<text class="countdown-label">支付剩余</text>
				<text class="countdown-time">{{ countdownText }}</text>
			</view>
		</view>

		<!-- 订单信息 -->
		<!-- <view class="order-info">
			<view class="order-row">
				<text>订单号：{{ order.orderNo }}</text>
				<text class="order-amount">应付: ￥{{ order.amount }}</text>
			</view>
		</view> -->

		<!-- 课程信息 -->
		<view class="course-card">
			<image :src="orderInfo.image" class="course-img" mode="aspectFill" />
			<!-- <view class="play-btn" @click="onPlayVideo">
				<uni-icons type="play-filled" color="#fff" size="36" />
			</view> -->
			<view class="course-info">
				<text class="course-title">{{ orderInfo.name }}</text>
				<text class="course-price">￥{{ orderInfo.price }}</text>
			</view>
		</view>
		<!-- 有效期和时间 -->
		<view class="valid-section">
			<view class="valid-row">
				<text>有效期</text>
				<text>365天</text>
			</view>
			<view class="valid-row">
				<text>时间</text>
				<text>{{ orderInfo.expireTime }}</text>
			</view>
		</view>

		<!-- 底部支付按钮 -->
		<view class="pay-btn-section">
			<button class="pay-btn" type="primary" @click="onPay">
				支付 {{ orderInfo.price }} 元
			</button>
		</view>
	</view>
</template>

<script>
import recordApi from '@/api/record.js'
export default {
	data() {
		return {
			orderInfo: {},
			countdown: 14 * 60 + 48, // 14分48秒，单位：秒
			countdownText: '',
			timer: null,
			courseId: ''
		};
	},
	onLoad(options) {
		this.courseId = options.courseId;
		this.startCountdown();
		this.getCourseInfo();
	},
	methods: {
		HTTP_IMG_URL() {
			return this.$imgUrl;
		},
		getCourseInfo() {
			recordApi.getInfo({
				id: this.courseId
			}).then((res) => {
				if (res.response.data.code == 1) {
					this.orderInfo = res.response.data.data.info;
					console.log(res.response.data.data.info, 'orderInfo');
				}
			})
		},
		onBack() {
			uni.navigateBack();
		},
		startCountdown() {
			this.updateCountdownText();
			this.timer = setInterval(() => {
				if (this.countdown > 0) {
					this.countdown--;
					this.updateCountdownText();
				} else {
					clearInterval(this.timer);
					this.countdownText = '已超时';
				}
			}, 1000);
		},
		updateCountdownText() {
			const m = String(Math.floor(this.countdown / 60)).padStart(2, '0');
			const s = String(this.countdown % 60).padStart(2, '0');
			this.countdownText = `${m}分${s}秒`;
		},
		onPlayVideo() {
			// TODO: 跳转到课程视频播放页或弹窗
			uni.showToast({
				title: '播放课程视频',
				icon: 'none',
			});
		},
		onPay() {
			uni.showLoading({
				title: '支付中...',
			});
			const _this = this;
			wx.login({
				success: function(res) {
					console.log(res, 'codecccceeeee');
					recordApi.getUserOpenid({
						code: res.code
					}).then(resu => {
						_this.payOrder();
					})
				},
				fail: function(res) {
					console.log('登陆失败');
				},
			})
		},
		payOrder() {
			let orderId = '';
			// 生成订单
			recordApi.addCourseOrder({
				id: this.courseId
			}).then(res1 => {
				if (res1.response.data.code == 1) {
					console.log(res1.response.data, 'res1.response.data');
					orderId = res1.response.data.data.order_id;
					// 支付订单
					recordApi.buyCourse({
						paytype: 'wechat',
						order_id: orderId,
						method: 'miniapp'
					}).then(res2 => {
						if(res2.response.data.code === 1) {
							let {timeStamp, nonceStr, package: packageParam, signType, paySign } = res2.response.data.data;
							wx.requestPayment({
								timeStamp,
								nonceStr,
								package: packageParam,
								signType,
								paySign,
								success(res) {
									console.log("res: ", res);
									uni.navigateTo({
										url:'/pages/pay/orderList'
									})
								},
								fail(res) {
									console.log('error', res);
								}
							})
							uni.hideLoading();
						}
					})
				}
			})
		}
	},
	onUnload() {
		if (this.timer) clearInterval(this.timer);
	},
};
</script>

<style lang="scss">
.pay-page {
	background: #f7f8fa;
	min-height: 100vh;
	padding-bottom: 80rpx;
}
.nav-bar {
	display: flex;
	align-items: center;
	height: 88rpx;
	padding: 0 24rpx;
	background: #fff;
	box-shadow: 0 2rpx 8rpx #f0f1f2;
	position: sticky;
	top: 0;
	z-index: 10;
}
.nav-title {
	flex: 1;
	text-align: center;
	font-size: 34rpx;
	font-weight: 500;
}
.status-section {
	margin: 32rpx 24rpx 0 24rpx;
	border-radius: 16rpx;
	padding: 32rpx 24rpx 16rpx 24rpx;
}
.status-row {
	display: flex;
	align-items: center;
}
.status-title {
	color: #222;
	font-size: 32rpx;
	font-weight: 600;
	margin-left: 12rpx;
}
.countdown-row {
	margin-top: 16rpx;
	background: #eaf2ff;
	color: #3478f6;
	border-radius: 24rpx;
	padding: 8rpx 24rpx;
	display: inline-block;
	font-size: 26rpx;
}
.order-info {
	margin: 24rpx 24rpx 0 24rpx;
	background: #fff;
	border-radius: 16rpx;
	padding: 24rpx;
}
.order-row {
	display: flex;
	justify-content: space-between;
	font-size: 28rpx;
	color: #222;
}
.order-amount {
	color: #ff3b30;
	font-weight: 600;
}
.course-card {
	margin: 24rpx 24rpx 0 24rpx;
	background: #fff;
	border-radius: 16rpx;
	padding: 0 0 24rpx 0;
	position: relative;
	overflow: hidden;
}
.course-img {
	width: 100%;
	height: 200rpx;
	border-radius: 16rpx 16rpx 0 0;
	object-fit: cover;
}
.play-btn {
	position: absolute;
	top: 80rpx;
	left: 50%;
	transform: translateX(-50%);
	background: rgba(0,0,0,0.3);
	border-radius: 50%;
	width: 72rpx;
	height: 72rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}
.course-info {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-top: 18rpx;
	padding: 0 24rpx;
}
.course-title {
	font-size: 30rpx;
	font-weight: 500;
}
.course-price {
	color: #ff3b30;
	font-size: 30rpx;
	font-weight: 600;
}
.valid-section {
	margin: 24rpx 24rpx 0 24rpx;
	background: #fff;
	border-radius: 16rpx;
	padding: 24rpx;
}
.valid-row {
	display: flex;
	justify-content: space-between;
	font-size: 28rpx;
	color: #222;
	margin-bottom: 8rpx;
}
.pay-btn-section {
	position: fixed;
	left: 0;
	right: 0;
	bottom: 0;
	background: transparent;
	padding: 24rpx;
	z-index: 20;
}
.pay-btn {
	width: 100%;
	height: 88rpx;
	background: linear-gradient(90deg, #3478f6, #2b6be6);
	color: #fff;
	font-size: 32rpx;
	border-radius: 44rpx;
	font-weight: 600;
	box-shadow: 0 4rpx 16rpx #3478f633;
}
</style>
